package com.example.demozy.service;

import com.example.demozy.entity.Challenge;
import com.example.demozy.entity.Participant;
import com.example.demozy.mapper.ChallengeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王成龙
 */
@Service
public class ChallengeService {
    // **************** 公开变量

    // **************** 私有变量

    @Autowired
    private ChallengeMapper challengeMapper;

    // **************** 继承方法

    // **************** 公开方法

    /**
     * 保存挑战赛
     *
     * @param challengeName
     * @param startTime
     * @param endTime
     * @param stockCode
     * @param startingFunds
     * @param maxParticipantsNumber
     * @return
     */
    public int saveChallenge(String challengeName, String introduction,
            Timestamp startTime, Timestamp endTime, String stockCode,
            double startingFunds, int maxParticipantsNumber) {
        return challengeMapper
                .insertChallenge(challengeName, introduction, startTime,
                                 endTime, stockCode, startingFunds,
                                 maxParticipantsNumber);
    }

    /**
     * 根据挑战赛id获取挑战赛
     *
     * @param challengeId
     * @return
     */
    public Challenge getChallengeById(int challengeId) {
        return challengeMapper.getChallengeById(challengeId);
    }

    /**
     * 获取最新的挑战赛id
     *
     * @return
     */
    public int getMaxChallengeId() {
        return challengeMapper.getMaxChallengeId();
    }

    /**
     * 获取所有正在进行的挑战赛
     *
     * @return
     */
    public List<Challenge> listChallengeInProgress() {
        return challengeMapper.listChallengesInProgress();
    }

    /**
     * 获取所有已结束的挑战赛
     *
     * @return
     */
    public List<Challenge> listChallengeEnded() {
        return challengeMapper.listChallengesEnded();
    }

    /**
     * 报名参加挑战赛
     *
     * @param challengeId
     * @param userId
     * @param conditionSheetId
     * @param startingFunds
     * @return
     */
    public int signForChallenge(int challengeId, int userId,
            String conditionSheetId, double startingFunds) {
        challengeMapper.updateChallengeById(challengeId);
        challengeMapper.insertParticipant(challengeId, userId, conditionSheetId,
                                          startingFunds, startingFunds);
        return 0;
    }

    /**
     * 根据挑战赛id获取当前参赛人数
     *
     * @param challengeId
     * @return
     */
    public int getParticipantsNumberById(int challengeId) {
        return challengeMapper.getParticipantsNumberById(challengeId);
    }

    /**
     * 根据挑战赛id获取最大参赛人数
     *
     * @param challengeId
     * @return
     */
    public int getMaxParticipantsNumberById(int challengeId) {
        return challengeMapper.getMaxParticipantsNumberById(challengeId);
    }

    /**
     * 检验该挑战赛有无此参赛用户id
     *
     * @param userId
     * @param challengeId
     * @return
     */
    public boolean isRepeated(int userId, int challengeId) {
        List<Participant> participantList =
                challengeMapper.listParticipantsById(challengeId);
        if (participantList.size() != 0) {
            for (Participant participant : participantList) {
                if (userId == participant.getUserId()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 根据挑战赛id返回参赛用户列表
     *
     * @param challengeId
     * @return
     */
    public List<Participant> listParticipantsById(int challengeId) {
        return challengeMapper.listParticipantsById(challengeId);
    }

    /**
     * 根据用户id和比赛状态（0，进行，1，结束）返回所参加的正在进行与已结束的挑战赛列表
     *
     * @param userId
     * @return
     */
    public List<Challenge> listChallengesByUserId(int userId,
            int challengeStat) {
        List<Integer> challengeIdList =
                challengeMapper.listChallengeIdByUserId(userId);
        List<Challenge> challengeList = new ArrayList<>();
        for (int challengeId : challengeIdList) {
            Challenge challenge = challengeMapper
                    .getChallengeUserById(challengeId, challengeStat);
            if (challenge != null) {
                challengeList.add(challengeMapper
                                          .getChallengeUserById(challengeId,
                                                                challengeStat));
            }
        }
        return challengeList;
    }

    /**
     * 挑战赛结束，挑战赛状态和用户参赛状态改变
     *
     * @return
     */
    public int endChallenge() {
        challengeMapper.updateChallengeByEndTime();
        challengeMapper.updateEntryStat();
        return 1;
    }

    /**
     * 根据挑战赛id和用户id获取参赛条件单id
     * @param challengeId
     * @param userId
     * @return
     */
    public String getConditionSheetIdByIds(int challengeId, int userId) {
        return challengeMapper.getConditionSheetIdByIds(challengeId, userId);
    }

    /**
     * 根据挑战赛id用户id更新用户收益率
     * @param challengeId
     * @param userId
     * @param yield
     * @return
     */
    public int updateYield(int challengeId, int userId, double yield) {
        return challengeMapper.updateYield(challengeId, userId, yield);
    }

    // **************** 私有方法

}
